{  12 March 2011. Individual Olympiad. Problem set: http://neerc.ifmo.ru/school/io/archive/20110312/problems-20110312-individual.pdf
	Problem A
	Points: 100
}
var f,r:boolean; s,t:string; i,n,j:longint;
begin
assign(input,'abbaa.in');
assign(output,'abbaa.out');
reset(input);
rewrite(output);
readln(n);
for i:=1 to n do begin
    readln(s);
    s:=s+'$$';
    if odd(length(s)) then s:=s+'$';
    r:=true;
    f:=false;
    j:=1;
    while (j<=length(s)) and (r) do begin
          t:=copy(s,j,2);
          if t='AA' then inc(j) else
          if (t='AB') or (t='BA') then inc(j,2) else
          if t[2]='$' then begin
             if t[1]='B' then r:=false;
             j:=length(s)+1;
          end else r:=false;
    end;
    if r then writeln('YES') else writeln('NO');
end;
close(input);
close(output);
end.